package org.wadael.waverobotfrmwrk;

import java.util.ArrayList;
import java.util.List;

import org.wadael.waverobotfrmwrk.utils.BlipUtils;

import com.google.wave.api.AbstractRobot;
import com.google.wave.api.Blip;
import com.google.wave.api.Wavelet;
import com.google.wave.api.event.BlipSubmittedEvent;
import com.google.wave.api.event.Event;
import com.google.wave.api.event.WaveletParticipantsChangedEvent;
import com.google.wave.api.impl.EventMessageBundle;

/**
 * 
 * Note : consider as the equivalent of RobotTemplateServlet from WRF v1
 * 
 * @author jerome
 * 
 */
@SuppressWarnings("serial")
public abstract class WadRobot extends AbstractRobot implements LimitedObedienceRobot {

	private List<String> authorisedUsers = new ArrayList<String>();
	
	/**
	 * Overriding Google's robot API
	 */
	@Override
	public void onBlipSubmitted(BlipSubmittedEvent event) {
		if(shouldObey(event)) 
			reactToSubmittedBlip(event.getBundle(), event.getBlip(), event);
		else
			whatToDoIfNotToObey(event.getBundle(), event.getBlip(), event);

	}

	/**
	 * from WRF v1
	 * 
	 * @param bundle
	 * @param blip
	 * @param event
	 */
	protected abstract void reactToSubmittedBlip(EventMessageBundle bundle, Blip blip, Event event);

	@Override
	public void onWaveletParticipantsChanged(WaveletParticipantsChangedEvent event) {
		reactToParticipantsChangement(event.getBundle(), event.getWavelet(), event);
	}

	/**
	 * From v1
	 * 
	 * 
	 * @param bundle
	 * @param wavelet
	 * @param e
	 */
	protected void reactToParticipantsChangement(EventMessageBundle bundle, Wavelet wavelet, Event e) {
		BlipUtils.appendNewBlipWithContent(e, getRobotSelfIntroduction() );
	}

	/**
	 * Equivalent to v1's  getDisplayName()
	 *
	 */
	@Override
	protected String getRobotName() {
		return getDisplayName();
	}
	
	
	/**
	 * Greets in French, English, Indian, Arab and Romanian
	 * 
	 * @since V1  
	 */
	protected String getRobotSelfIntroduction() {
		return "Bonjour! Hello ! Namaste ! Salam ! buna ziua !";
	}

	/**
	 * Hell, I cannot remember where it is displayed
	 * 
	 * @since V1
	 * @return
	 * @deprecated
	 */
	protected String getDisplayName() {
		return "Robot Made w/ WadRobotFramework";
	}
		
	public void setAuthorisedUsers(List<String> authorisedUsers) {
		this.authorisedUsers = authorisedUsers;
	}
	
	public List<String> getAuthorisedUsers() {
		return authorisedUsers;
	}	
	
	/**
	 * See subclasses for implementations that actually check something.
	 *  
	 * @param event
	 * @return true by default
	 * @since 20100507 for limited obedience feature
	 */
	public boolean shouldObey(Event event) {
		return true;
	}
	
	/**
	 * Default implementation : empty
	 * @param bundle
	 * @param blip
	 * @param event
	 * @since 20100507 for limited obedience feature
	 */
	public void whatToDoIfNotToObey(EventMessageBundle bundle, Blip blip, Event event){
		
	}

	
}
